document.getElementById('login-button').addEventListener('click', function () {
    const user_no = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    if (!user_no || !password) {
        alert('用户名和密码不能为空');
        return;
    }

    fetch('/api/token/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            user_no: user_no,
            password: password
        })
    })
    .then(response => response.json())
    .then(data => {
        if (data.access) {
            localStorage.setItem('access_token', data.access);
            localStorage.setItem('refresh_token', data.refresh);
            localStorage.setItem('user_id', user_no);
            window.location.href = '/main_page/';
        } else {
            alert('登录失败：用户名或密码错误');
        }
    })
    .catch(error => {
        console.error('请求失败:', error);
        alert('登录请求失败，请稍后再试');
    });
});

// 注册按钮
document.getElementById('register-button').addEventListener('click', function () {
    window.location.href = '/register/';
});
